<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/


// ETATS DOMAINES HEBERG
// 1	OK
// 2	Modifié
// 3 	Création
// 4	Suppression
session_start();
if(isset($_POST['ajax'])) {
	define('IN', '1');
	define('REQUEST_AJAX', '1');
	include('../php/lib/common.php');
	include('../php/lib/panel_inc.php');
	connexionbdd();
	$tpl = new gagatemplate('../');
	$id = intval($_POST['id_heberg']);
	$infos_heberg = mysql_query("SELECT comptes_heberg.id,
										comptes_heberg.u_nic,
										comptes_heberg.uid_linux,
										comptes_heberg.user_linux,
										comptes_heberg.serveur_apache,
										comptes_heberg.offre,
										comptes_heberg.etat,
										comptes_heberg.doms  AS c_doms,
										offre_heberg.doms AS o_doms_,
										offre_surmesure.doms AS o_doms
										FROM comptes_heberg 
										LEFT JOIN offre_heberg
										ON comptes_heberg.offre = offre_heberg.id_offre
										LEFT JOIN offre_surmesure
										ON comptes_heberg.id = offre_surmesure.id_heberg
										WHERE comptes_heberg.id='".$id."' AND comptes_heberg.u_nic='".$_SESSION['u_nic']."'")or die(mysql_error());
	if(mysql_num_rows($infos_heberg) == 1) {
		$infos_heberg = mysql_fetch_array($infos_heberg);
		if($infos_heberg['o_doms'] == "") {
			$infos_heberg['o_doms'] = $infos_heberg['o_doms_'];
		}
		if($_POST['action'] == "add") {
			// Formulaire domaine acheté sur BH
			if($_POST['to'] == "dom_buy") {
				if(($infos_heberg['c_doms'] < $infos_heberg['o_doms'])||($infos_heberg['o_doms'] == '')) {
					$liste_doms = query("SELECT id, domaine FROM comptes_domain WHERE u_nic = '".$_SESSION['u_nic']."' AND heberg_lie = '0'");
					While($doms = mysql_fetch_array($liste_doms)) {
						$tpl->assign_array('dom_select', array(
							'id' => $doms['id'],
							'dom' => $doms['domaine'],
						));				
					}
					$tpl->assign(array('prod_id' => $id));
					$tpl->parse('panel-hebergement-doms-add-bhbuy.html');
				}
				else {
					echo '<div class="attention">Vous avez atteint le quota de domaines</div>';
				}
			}
			// Envoi POST AJAX domaine acheté sur BH
			elseif($_POST['to'] == "add_bhbuy_post") {
				$dom = query("SELECT 
								comptes_domain.*,
								users.reg_contact_id,
								users.u_nic
								FROM comptes_domain
								LEFT JOIN users
								ON comptes_domain.u_nic = users.u_nic
								WHERE comptes_domain.id='".intval($_POST['id_dom'])."' AND comptes_domain.u_nic = '".$_SESSION['u_nic']."'");
				if(mysql_num_rows($dom) == 1) {
					$dom = mysql_fetch_array($dom);
					$exist = mysql_fetch_array(query("SELECT COUNT(*) AS verif FROM hebergdata_doms WHERE domaine='".$dom['domaine']."'"));
					if($exist['verif'] == 0) {
						// Dossier
						$dossier = preg_replace('#([^a-zA-Z0-9\-\_/])+#i', '', $_POST['dossier']);
						if(!substr($dossier,0,1) == "/") { $dossier = "/".$dossier; }
						// Ajout du domaine
						query("INSERT INTO hebergdata_doms(id_dom_bh, u_nic, dossier, domaine, etat, id_heberg) VALUES('".intval($_POST['id_dom'])."', '".$_SESSION['u_nic']."', '".$dossier."', '".$dom['domaine']."', 3, '".$infos_heberg['id']."')");
						// MAJ quota
						query("UPDATE comptes_heberg SET doms = doms+1 WHERE id='".$id."'");
						// Changement des serveurs DNS
						include ROOT.'php/lib/registrars/'.$registrar[$dom['id_registrar']].'.inc.php';
						$_domgest = new bh_doms_gest();
						$dom_info = $_domgest->dom_infos($infos_dom['domaine']);	
						$_domgest->deconnect();
						$update['dns1'] = "ns2.breizh-heberg.com";
						$update['dns2'] = "ns3.breizh-heberg.com";
						$update['dns3'] = "";
						$update['dns4'] = "";
						$update['reg_contact_id'] = $infos_dom['reg_contact_id'];
						if($infos_dom['lock'] == 1) {
							$update['registry_status'] = 64;
						}
						else {
							$update['registry_status'] = 0;
						}
						
						// Zone DNS -- Suppression de l'ancienne zone et création nouvelle
						// 	Vérification existante
						if($dom['zone_dns'] == 0) {
							query("INSERT INTO zones_dns(id_domain, etat) VALUES('".intval($_POST['id_dom'])."', 3)");
						}
						$id_zone = mysql_fetch_array(query("SELECT id FROM zones_dns WHERE id_domain = '".intval($_POST['id_dom'])."'"));
						query("DELETE FROM zones_dns_content WHERE id_zone  = '".$id_zone['id']."'");
						// Entrées de base
						query("INSERT INTO zones_dns_content
											(entree, type, value_mx, value, id_zone, lie_heberg) 
											VALUES
											('ftp', 'A', '', '".$server[$infos_heberg['serveur_apache']]['ip']."', '".$id_zone['id']."', '1'),
											('www', 'A', '', '".$server[$infos_heberg['serveur_apache']]['ip']."', '".$id_zone['id']."', '1'),
											('@', 'A', '', '".$server[$infos_heberg['serveur_apache']]['ip']."', '".$id_zone['id']."', '1'),
											('@', 'MX', '1', 'mail.breizh-heberg.net', '".$id_zone['id']."', '1')");
						
						if(($dom['dns1'] != "ns2.breizh-heberg.com") OR ($dom['dns2'] != "ns3.breizh-heberg.com")) {
							$tid = $_domgest->dom_update($infos_dom['domaine'],$update);
							query("UPDATE comptes_domain SET zone_dns = '".$id_zone['id']."', dns1='ns2.breizh-heberg.com', dns2='ns3.breizh-heberg.com', dns3='', dns4='', tid='$tid', etat = '7', heberg_lie='$id' WHERE id = '".intval($_POST['id_dom'])."'");
						}
						else {
							query("UPDATE comptes_domain SET zone_dns = '".$id_zone['id']."', dns1='ns2.breizh-heberg.com', dns2='ns3.breizh-heberg.com', dns3='', dns4='', heberg_lie='$id' WHERE id = '".intval($_POST['id_dom'])."'");
						}
						query("UPDATE zones_dns SET etat = 3 WHERE id_domain = '".intval($_POST['id_dom'])."'");
						
						echo'<div class="green">
							Le domaine a bien été ajouté.
							</div><div style="text-align:right"><a href="hebergement-doms.html?id='.$id.'">&#187; Retour</a></div>';
					}
					else {
						echo'<div class="attention">
							Le domaine est déjà lié à un hébergement.
							</div><div style="text-align:right"><a href="hebergement-doms.html?id='.$id.'">&#187; Retour</a></div>';
					}
				}
				else {
					echo'<div class="attention">
						Le domaine est déjà lié à un hébergement.
						</div><div style="text-align:right"><a href="hebergement-doms.html?id='.$id.'">&#187; Retour</a></div>';
				}
			}
			// Ajout d'un domaine externe à BH
			elseif($_POST['to'] == "dom_externe") {
				if(($infos_heberg['c_doms'] < $infos_heberg['o_doms'])||($infos_heberg['o_doms'] == '')) {
					$tpl->assign(array('prod_id' => $id));
					$tpl->parse('panel-hebergement-doms-add-externe.html');
				}
				else {
					echo '<div class="attention">Vous avez atteint le quota de domaines</div>';
				}
			}
			// Post dom externe à BH
			elseif($_POST['to'] == "dom_externe_post") {
				if(($infos_heberg['c_doms'] < $infos_heberg['o_doms'])||($infos_heberg['o_doms'] == '')) {
					if (preg_match(REGEX_DOMS, $_POST['domaine'])) {
					$exist = mysql_fetch_array(query("SELECT COUNT(*) AS exist FROM hebergdata_doms WHERE domaine = '".$_POST['domaine']."'"));
					$exist2 = mysql_fetch_array(query("SELECT COUNT(*) AS exist FROM comptes_domain WHERE domaine = '".$_POST['domaine']."'"));
						if(($exist['exist'] == 0)AND($exist2['exist'] == 0)) {
							// Dossier
							$dossier = preg_replace('#([^a-zA-Z0-9\-\_/])+#i', '', $_POST['dossier']);
							if(!substr($dossier,0,1) == "/") { $dossier = "/".$dossier; }
							// Ajout du domaine
							query("INSERT INTO hebergdata_doms(id_dom_bh, u_nic, dossier, domaine, etat, id_heberg) VALUES(0, '".$_SESSION['u_nic']."', '".$dossier."', '".$_POST['domaine']."', 3, '".$infos_heberg['id']."')");
							// MAJ quota
							query("UPDATE comptes_heberg SET doms = doms+1 WHERE id='".$id."'");
							// --
							echo'<div class="green">
								Le domaine a bien été ajouté.
								</div><div style="text-align:right"><a href="hebergement-doms.html?id='.$id.'">&#187; Retour</a></div>';

						}
						else {
							echo'<div class="attention">
								Le domaine est déjà lié à un hébergement Breizh Héberg.
								</div><div style="text-align:right"><a href="#" onclick="heb_dom(\'dom_externe\',\'add\',\''.$id.'\'); return false;">&#187; Retour</a></div>';
						}
					}
					else {
						echo'<div class="attention">
							Le format du domaine est incorrect.
							</div><div style="text-align:right"><a href="#" onclick="heb_dom(\'dom_externe\',\'add\',\''.$id.'\'); return false;">&#187; Retour</a></div>';
					}
				}
			}
			// Ajout d'unalias dom BH
			elseif($_POST['to'] == "dom_alias_bh") {
				if(($infos_heberg['c_doms'] < $infos_heberg['o_doms'])||($infos_heberg['o_doms'] == '')) {
					reset($alias_bh);
					while (list($id_alias, $ext_alias) = each($alias_bh)) {
						$tpl->assign_array('alias_bh_assign', array(
							'ext' => $ext_alias,
							'id' => $id_alias
						));
					}
					$tpl->assign(array('prod_id' => $id));
					$tpl->parse('panel-hebergement-doms-add-alias-bh.html');
				}
				else {
					echo '<div class="attention">Vous avez atteint le quota de domaines</div>';
				}
			}
			// Post alias BH
			elseif($_POST['to'] == "dom_alias_bh_post") {
				if(($infos_heberg['c_doms'] < $infos_heberg['o_doms'])||($infos_heberg['o_doms'] == '')) {
					$ext_alias = intval($_POST['ext_alias']);
					$alias = preg_replace('#([^a-z0-9\-\_])+#i', '', $_POST['alias']);
					if($alias_bh[$ext_alias] != '') {
						//existe déjà?
						$exist = mysql_fetch_array(query("SELECT COUNT(*) AS exist FROM hebergdata_doms WHERE domaine = '".$alias."' AND id_alias_bh = '".$ext_alias."'"));
						if($exist['exist'] == 0) {
							// Dossier
							$dossier = preg_replace('#([^a-zA-Z0-9\-\_/])+#i', '', $_POST['dossier']);
							if(!substr($dossier,0,1) == "/") { $dossier = "/".$dossier; }
							query("INSERT INTO hebergdata_doms(id_dom_bh, u_nic, dossier, domaine, id_alias_bh,  etat, id_heberg) VALUES(0, '".$_SESSION['u_nic']."', '".$dossier."', '".$alias."', '".$ext_alias."', 3, '".$infos_heberg['id']."')");
							// MAJ quota
							query("UPDATE comptes_heberg SET doms = doms+1 WHERE id='".$id."'");
							// --
							echo'<div class="green">
									L\'alias domaine Breizh Héberg a bien été ajouté.
									</div><div style="text-align:right"><a href="hebergement-doms.html?id='.$id.'">&#187; Retour</a></div>';
						}
						else {
							echo'<div class="attention">
									Cet alias domaine Breizh Héberg est déjà pris.
									</div><div style="text-align:right"><a href="#" onclick="heb_dom(\'dom_alias_bh\',\'add\',\''.$id.'\'); return false;">&#187; Retour aux Alias domaine</a></div>';

						}
					}				
				}
			}
		}
		elseif($_POST['action'] == 'gest') {
			if($_POST['to'] == "front") {
					$liste_doms = query("SELECT id, id_dom_bh, id_alias_bh, domaine, dossier, etat FROM hebergdata_doms WHERE u_nic = '".$_SESSION['u_nic']."' AND id_heberg = '".$infos_heberg['id']."'");
					While($doms = mysql_fetch_array($liste_doms)) {
						if($doms['id_alias_bh'] != 0) {
							$domaine = $doms['domaine'].'.'.$alias_bh[$doms['id_alias_bh']];
						}
						else {
							$domaine = $doms['domaine'];
						}
						$tpl->assign_array('liste_doms', array(
							'id' => $doms['id'],
							'id_dom_bh' => $doms['id_dom_bh'],
							'dossier' => $doms['dossier'],
							'domaine' => $domaine,
							'etat' => $doms['etat'],
							'id_alias_bh' => $doms['id_alias_bh']
						));				
					}
					$tpl->assign(array('prod_id' => $id));
					$tpl->parse('panel-hebergement-doms-front.html');
			}
		}
		elseif($_POST['action'] == 'dom_del') {
			$id_dom = intval($_POST['id_dom']);
			$check = query("SELECT * FROM hebergdata_doms WHERE u_nic = '".$_SESSION['u_nic']."' AND id = '".$id_dom."'");
			if(mysql_num_rows($check) == 1) {
				$check = mysql_fetch_array($check);
				query("DELETE FROM hebergdata_doms WHERE id = '".$id_dom."'");
				// MAJ quota
				query("UPDATE comptes_heberg SET doms = doms-1 WHERE id='".$id."'");
				query("UPDATE comptes_domain SET heberg_lie = 0 WHERE id = '".$check['id_dom_bh']."'");
				echo'<div class="green">
						Le domaine sera supprimé d\'ici quelques minutes.
					</div><div style="text-align:right"><a href="hebergement-doms.html?id='.$id.'">&#187; Retour</a></div>';

			}
			else {
				echo '<div class="attention">Fatal Error</div>';
			}
		}
		elseif($_POST['action'] == 'ss_doms') {
			$id_dom = intval($_POST['id_dom']);
			$infos_dom = query("SELECT domaine,dossier FROM hebergdata_doms WHERE u_nic = '".$_SESSION['u_nic']."' AND id = '".$id_dom."'");
			if(mysql_num_rows($infos_dom) == 1) {
				$infos_dom = mysql_fetch_array($infos_dom);
				$_ss_doms = query("SELECT * FROM hebergdata_ssdoms WHERE id_dom = '".$id_dom."'");
				$tpl->assign(array(
					'domaine' => $infos_dom['domaine'],
					'dom_id' => $id_dom,
					'dossier' => $infos_dom['dossier'],
					'prod_id' => $id
				));
				While($ss_doms = mysql_fetch_array($_ss_doms)) {
					$tpl->assign_array('ss_doms', array(
						'id' => $ss_doms['id'],
						'dossier' => $ss_doms['dossier'],
						'ss_dom' => $ss_doms['ss_dom'],
					));
				}				
				$tpl->parse('panel-hebergement-doms-ssdoms-front.html');
			}
			else {
				echo '<div class="attention">Fatal Error</div>';
			}
		}
		elseif($_POST['action'] == 'add_ss_dom') {
			// Ajouter un sous domaine - affichage form
			$id_dom = intval($_POST['id_dom']);
			$infos_dom = query("SELECT domaine FROM hebergdata_doms WHERE u_nic = '".$_SESSION['u_nic']."' AND id = '".$id_dom."'");
			if(mysql_num_rows($infos_dom) == 1) {
				$infos_dom = mysql_fetch_array($infos_dom);
				$tpl->assign(array(
					'domaine' => $infos_dom['domaine'],
					'dom_id' => $id_dom,
					'dossier' => $infos_dom['dossier'],
					'prod_id' => $id
				));
				$tpl->parse('panel-hebergement-doms-ssdoms-add.html');
			}
			else {
				echo '<div class="attention">Fatal Error</div>';
			}
		}
		elseif($_POST['action'] == 'add_ss_dom_post') {
			// Ajouter un sous domaine - traitement
			$id_dom = intval($_POST['id_dom']);
			$dossier = preg_replace('#([^a-zA-Z0-9\-\_/])+#i', '', $_POST['dossier']);
			$ss_dom = preg_replace('#([^a-z0-9\-\.])+#i', '', strtolower($_POST['ss_dom']));
			$infos_dom = query("SELECT domaine, id_dom_bh, id FROM hebergdata_doms WHERE u_nic = '".$_SESSION['u_nic']."' AND id = '".$id_dom."'");
			if(mysql_num_rows($infos_dom) == 1) {
				$exist = mysql_fetch_array(query("SELECT COUNT(*) AS verif FROM hebergdata_ssdoms WHERE id_dom = '".$id_dom."' AND ss_dom = '".$ss_dom."'"));
				$infos_doms = mysql_fetch_array($infos_dom);
				if(($exist['verif'] == 0)&&($ss_dom != "")) {
					if($infos_doms['id_dom_bh'] != 0) {
						$zone = mysql_fetch_array(query("SELECT * FROM zones_dns WHERE id_domain = '".$infos_doms['id_dom_bh']."'"));
						query("DELETE FROM zones_dns_content WHERE entree = '".$ss_dom."' AND id_zone = '".$zone['id']."'");
						query("INSERT INTO zones_dns_content(entree, type, value, id_zone) VALUES('".$ss_dom."', 'A', '".$server[$infos_heberg['serveur_apache']]['ip']."', '".$zone['id']."')");
						query("UPDATE zones_dns SET etat = 3 WHERE id = '".$infos_heberg['id_dom_bh']."'");
					}
					query("INSERT INTO hebergdata_ssdoms(id_dom, ss_dom, dossier) VALUES('".$infos_doms['id']."', '".$ss_dom."', '".$dossier."')");
					query("UPDATE hebergdata_doms SET etat = 3 WHERE id = '".$infos_doms['id']."'");
					echo'<div class="green">
							Le sous-domaine a bien été ajouté, les modifications prendront effet d\'ici 15 minutes.
						</div><div style="text-align:right"><a href="#" onclick="heb_dom(\''.$id_dom.'\',\'ss_doms\',\''.$id.'\'); return false;">&#187; Retour</a></div>';
				}
				else {
					echo '<div class="attention">
							Le sous-domaine existe déjà.
							</div><div style="text-align:right"><a href="#" onclick="heb_dom(\''.$id_dom.'\',\'ss_doms\',\''.$id.'\'); return false;">&#187; Retour</a></div>';
				}
			}
			else {
				echo '<div class="attention">Fatal Error</div>';
			}
		}
		
		elseif($_POST['action'] == 'ss_dom_del') {
			// Supprimer un sous domaine
			$id_ssdom = intval($_POST['id_dom']);
			$infos_dom = mysql_fetch_array(query("SELECT 
								hebergdata_ssdoms.id_dom,
								hebergdata_ssdoms.id,
								hebergdata_doms.u_nic,
								hebergdata_doms.id,
								hebergdata_doms.id_dom_bh
								FROM hebergdata_ssdoms
								LEFT JOIN hebergdata_doms
								ON hebergdata_ssdoms.id_dom = hebergdata_doms.id
								WHERE hebergdata_ssdoms.id = '".$id_ssdom."'"));
			if($infos_dom['u_nic'] == $_SESSION['u_nic']) {
				query("DELETE FROM hebergdata_ssdoms WHERE id = '".$id_ssdom."'");
				query("UPDATE hebergdata_doms SET etat = 3 WHERE id = '".$infos_dom['id_dom']."'");
				echo'<div class="green">
							Le sous-domaine a bien été supprimé. Les modifications prendront effet d\ici 15 minutes.
						</div><div style="text-align:right"><a href="#" onclick="heb_dom(\''.$infos_dom['id_dom'].'\',\'ss_doms\',\''.$id.'\'); return false;">&#187; Retour</a></div>';

			}
			else {
				echo '<div class="attention">Fatal Error</div>';
			}
		}
		
		elseif($_POST['action'] == 'dom_edit') {
			// Modifie un ndd
			$id_dom = intval($_POST['id_dom']);
			$infos_dom = mysql_fetch_array(query("SELECT * FROM hebergdata_doms	WHERE id = '".$id_dom."'"));
			if($infos_dom['u_nic'] == $_SESSION['u_nic']) {
				$tpl->assign(array(
					'ndd' => $infos_dom['domaine'],
					'dom_id' => $infos_dom['id'],
					'dossier' => $infos_dom['dossier'],
					'prod_id' => $id
				));
				$tpl->parse('panel-hebergement-doms-edit.html');
			}
			else {
				echo '<div class="attention">Fatal Error</div>';
			}
		}
		
		elseif($_POST['action'] == 'dom_edit_post') {
			// Modifie un ndd
			$id_dom = intval($_POST['id_dom']);
			$infos_dom = mysql_fetch_array(query("SELECT * FROM hebergdata_doms	WHERE id = '".$id_dom."'"));
			if($infos_dom['u_nic'] == $_SESSION['u_nic']) {
				// Dossier
				$dossier = preg_replace('#([^a-zA-Z0-9\-\_/])+#i', '', $_POST['dossier']);
				if(!substr($dossier,0,1) == "/") { $dossier = "/".$dossier; }
				query("UPDATE hebergdata_doms SET etat = 3, dossier = '".$dossier."' WHERE id =  '".$id_dom."'");
				echo'<div class="green">
							Le dossier de base a bien été changé, les modifications prendront effet d\'ici 15 minutes.
						</div><div style="text-align:right"><a href="#" onclick="heb_dom(\'front\',\'gest\',\''.$id.'\'); return false;">&#187; Retour</a></div>';

			}
			else {
				echo '<div class="attention">Fatal Error</div>';
			}
		}
	}
	else {
		echo '<div class="attention">Fatal Error</div>';
	}
}
?>
